var _csrf;

$(document).ready(function () {
    _csrf = yii.getCsrfToken();

    // $(document).on('click', '#ticket-print-touch', function () {
    //     window.top.$('iframe[name=iframe-print]')[0].contentWindow.ticketPrint();
    // });
    // $(document).on('click', '#ticket-print-touch', function () {
    //     $(this).removeAttr("id");
    //     var url1 = $(this).data('url1');
    //     var id = $(this).data('id');
    //     var flag = false;
    //     if (url1 != '' && id green!= '') {
    //         $.ajax({
    //             type: "post",
    //             url: url1,
    //             async: false,
    //             cache: false,
    //             data: {ajax: 'status', id: id, _csrf: yii.getCsrfToken()},
    //             dataType: "json",
    //             success: function (data) {
    //                 var newdata = data;
    //                 if (newdata == 1) {
    //                     flag = true;
    //                 }
    //                 else if (newdata == 2) {
    //                     alert('此课程1小时内已出票！');
    //                 }
    //                 else if (newdata ==3) {
    //                     $(this).attr("id", 'ticket-print-touch');
    //                 }
    //             }
    //         });
    //     }
    //     if (flag) {
    //         window.top.$('iframe[name=iframe-print]')[0].contentWindow.ticketPrint();
    //         flag = false;
    //         $(".index_banner").hide();
    //         $(".cj-tc").hide();
    //         $(".cj-zz").hide();
    //     }
    //
    // });
    function ticketPrints() {
        alert('开始打印');
        window.top.$('iframe[name=iframe-print]')[0].contentWindow.ticketPrint();
    }
    $(document).on('click', '.cj-lock-confirm', function () {
        var url1 = $(this).data('url1');
        var id = $(this).data('id');
        var ids = $(this).data('ids');
        var class_students= $(this).data('class_student');
        var backstage_id=$("#backstage_id").val();
        var flag = false;
        if (url1 != '' && id != '') {
            $.ajax({
                type: "post",
                url: url1,
                async: false,
                cache: false,
                data: {ajax: 'status', id: id,ids:ids,class_students:class_students, _csrf: yii.getCsrfToken()},
                dataType: "json",
                success: function (data) {
                    var newdata = data;
                    if (newdata == 1) {//如果是1，就可以签到，并且签到完成之后弹窗在几秒之后消失
                        ticketPrints();
                        $('.cj-lock-content').hide();
                        $('.cj-last-alert').show();
                        //点击确认打卡之后，出来的是确认完成的弹窗
                        $('.cj-last-alert img').click(function(){
                            $('.cj-last-alert').hide();
                            $("#login").focus();
                        });
                        //确认完成弹窗中，点击上面的小太阳，弹出框隐藏
                        $('.cj-last-alert .red').click(function(){
                            $('.cj-last-alert').hide();
                            $("#login").focus();
                        });
                        //确认完成弹窗中，点击红色的继续打卡按钮，弹出框隐藏，并且输入框获得焦点
                        $('.cj-last-alert .green').click(function(){
                            $('.cj-last-alert').hide();
                            $("#login").focus();
                        });
                        $('.cj-last-alert').delay(4000).hide(0);
                    }
                    //确认完成弹窗中，点击绿色的返回，弹出框隐藏，并且返回主页面
                    else if (newdata == 2) {//如果是2，说明这个课程在1个小时只能已经出票
                        alert('此课程一个小时之内已经签到，不能重复签到！');
                        $('.cj-lock-content').delay(2500).hide(0);
                        $("#login").focus();

                    }else if(newdata == 3){
                        alert('测试打印开始');
                        $('.cj-lock-content').hide();
                        ticketPrints();

                    }
                }
            });
        }
    });

    $(document).on('click', '.close-img1', function () {
        $(".index_banner").hide();
        $(".cj-tc").hide();
        $(".cj-zz").hide();
    });
    $(window).keydown(function (event) {
        var card_id = $("#login").val();
        if (event.keyCode == 13 && card_id != '') {
            if(card_id != ''){
                $.post(
                    $('#card_route').val(), {
                        card_id: card_id,
                        ajax: 'attr',
                        _csrf: yii.getCsrfToken()
                    },function (data) {
                        //console.log(data);
                        if(data.content != '' && data.data != '卡号不存在' && data.data != '您没课'){
                            $('.cj-lock-content').show();
                            $('.cj-lock-content').html(data.content);
                            $('#iframe-print').attr('src',data.data.print_url);
                            //console.log(data.data);
                            if(data.data==0){
                                $('.cj-lock-content').delay(4000).hide(0);//如果是已经打卡则让弹窗在几秒钟之内消失
                            }
                        }else if (data.data == '您没课') {
                            $('.cj-lock-content').show();
                            $('.cj-lock-content').html(data.content);
                            $('.cj-lock-content').delay(4000).hide(0);
                            $('.cj-clock-sun').click(function(){
                                $('.cj-lock-content').hide();
                            });
                            $('.cj-clock-cardno').delay(4200).hide(0);
                        }else if(data.data == '卡号不存在'){
                            $('.cj-clock-cardno').show();
                            $('.cj-clock-cardno').html(data.content);
                            $('.cj-clock-sun').click(function(){
                                $('.cj-clock-cardno').hide();
                            });
                            $('.cj-clock-cardno').delay(4000).hide(0);
                        }else if (data.data.id== '测试卡号'){
                            $('.cj-lock-content').show();
                            $('.cj-lock-content').html(data.content);
                            $('#iframe-print').attr('src',data.data.print_url);
                        }
                    })
            }
            $("#login").val('');
        }
    });

    var timer;
    $("#main_image").touchSlider({
        flexible: true,
        speed: 700,
        btn_prev: $("#btn_prev"),
        btn_next: $("#btn_next"),
        paging: $(".flicking_con a"),
        counter: function (e) {
            $(".flicking_con a").removeClass("on").eq(e.current - 1).addClass("on");
        }
    });

    timer = setInterval(function () {
        $("#btn_next").click();
    }, 5000);

    if (window.navigator.msPointerEnabled) {    /*Events for IE only*/
        document.getElementById("main_visual").addEventListener("MSPointerDown", function (e) {
            clearInterval(timer);
        });
        document.getElementById("main_visual").addEventListener("MSPointerUp", function (e) {
            timer = setInterval(function () {
                $("#btn_next").click();
                $("#login").focus();
            }, 5000);
        });
    }

    $("#main_image").bind("touchstart", function (e) {
        clearInterval(timer);
    }).bind("touchend", function (e) {
        timer = setInterval(function () {
            $("#btn_next").click();
            $("#login").focus();
        }, 5000);
    });

    $(document).on('click', '*', function (e) {
        $("#login").focus();
    });

    $("#login").focus();


    /************************************************************************新修改的选择条件的js代码 begin**************************************************/
//点击选择课程分类 begin
    $(document).on('click','#category-select-popup',function(){
        $('.cj-touch-select').show();
        $('.cj-select-back').show();
        $('.cj-select-category').show();
        $('.cj-con-list ul li').click(function(){
            $('.cj-category-ionic').removeClass('span-active');
            $(this).parent().parent().children('h2').children('.cj-category-ionic').addClass('span-active');
            $('.cj-con-list ul li').removeClass('active-li');
            $(this).addClass('active-li').siblings().removeClass('active-li');
            var course_category =$(this).text();
            if(course_category=='全部'){
                course_category=$(this).parent().parent().children('h2').text()
            }
            $('#category-select-popup').text(course_category);
            $('#category-select-popup').addClass('active-bot');
            $('.cj-select-back').hide();
            $('.cj-select-category').hide();
            $('.cj-touch-select').hide();
        });
        $('.cj-category-top').children('button').first().click(function(){
            $('.cj-touch-select').hide();
            $('.cj-select-back').hide();
            $('.cj-select-category').hide()
        });//这是哪个返回的按钮
        $('.cj-top-bot-hide').click(function(){
            $('.cj-category-ionic').removeClass('span-active');
            $('#category-select-popup').text($(this).text());
            $('#category-select-popup').addClass('active-bot');
            $('.cj-con-list ul li').removeClass('active-li');
            $('.cj-touch-select').hide();
            $('.cj-select-back').hide();
            $('.cj-select-category').hide()
        })//全部按钮的点击

    });
//点击选择课程分类 end
//点击选择授课方式 begin
    $(document).on('click','#way-select-popup',function(){
        $('.cj-touch-select').show();
        $('.cj-select-back').show();
        $('.cj-select-way').show();
        $('.cj-select-way ul li').click(function(){
            $(this).addClass('active-li').siblings().removeClass('active-li');
            var course_way=$(this).text();
            $('#way-select-popup').text(course_way);
            $('#way-select-popup').addClass('active-bot');
            $('.cj-select-back').hide();
            $('.cj-select-way').hide();
            $('.cj-touch-select').hide();

        });
        $('.cj-category-top').children('button').click(function(){
            $('.cj-touch-select').hide();
            $('.cj-select-back').hide();
            $('.cj-select-way').hide()
        });
    });
//点击选择授课方式 end
//点击选择上课时间 begin
    $(document).on('click','#time-select-popup',function(){
        $('.cj-touch-select').show();
        $('.cj-select-back').show();
        $('.cj-select-time').show();
        $('.cj-select-time ul li').click(function(){
            $(this).addClass('active-li').siblings().removeClass('active-li');
            var course_time=$(this).text();
            $('#time-select-popup') .text(course_time);
            $('#time-select-popup').addClass('active-bot');
            $('.cj-select-back').hide();
            $('.cj-select-time').hide();
            $('.cj-touch-select').hide();
        });
        $('.cj-category-top').children('button').click(function(){
            $('.cj-touch-select').hide();
            $('.cj-select-back').hide();
            $('.cj-select-time').hide()
        });

    });
//点击选择上课时间 end
//点击选择上午/下午 begin
    $(document).on('click','#noon-select-popup',function(){
        $('.cj-touch-select').show();
        $('.cj-select-back').show();
        $('.cj-select-noon').show();
        $('.cj-select-noon ul li').click(function(){
            $(this).addClass('active-li').siblings().removeClass('active-li');
            var course_noon=$(this).text();
            $('#noon-select-popup').text(course_noon);
            $('#noon-select-popup').addClass('active-bot');
            $('.cj-select-back').hide();
            $('.cj-select-noon').hide();
            $('.cj-touch-select').hide();
        });
        $('.cj-category-top').children('button').click(function(){
            $('.cj-touch-select').hide();
            $('.cj-select-back').hide();
            $('.cj-select-noon').hide()
        });
    });
//点击选择上午/下午 end
    /************************************************************************新修改的选择条件的js代码 end**************************************************/
    /**********************************************************************新修改课程列表页面ajax代码 begin*************************************************/
//下面是查询的ajax请求
    function getUrl(url) {
        var  course_category = $('#category-select-popup').text();
        var  class_modality = $('#way-select-popup').text();
        var  course_time = $('#time-select-popup').text();
        var  course_noon=$('#noon-select-popup').text();
        switch(course_time){
            case '周一':
                course_time='1';
                break;
            case '周二':
                course_time='2';
                break;
            case '周三':
                course_time='3';
                break;
            case '周四':
                course_time='4';
                break;
            case '周五':
                course_time='5';
                break;
            case '周六':
                course_time='6';
                break;
            case '周天':
                course_time='7';
        }
        switch (course_noon){
            case '上午':
                course_noon=0;
                break;
            case '下午':
                course_noon=1300;

        }
        $.post(url, {
            course_category:course_category,
            class_modality:class_modality,
            course_time: course_time,
            course_noon:course_noon,
            ajax: 'course',
            _csrf: yii.getCsrfToken()
        }, function (data) {
            if (data.content != undefined) {
                $('#load').hide();
                $('#itemContainer').html(data.content);
                $('#page').html(data.pages);
            }
        });
    }
    //点击分页之后的效果，调用查询课程的ajax
    $(document).on('click', '.pagination li a', function () {
        $(this).css({"color": "#d40000"});
    });
    $(document).on('click', '.pagination li a', function (e) {
        e.preventDefault();
        if ($(this).attr('href').length > 0) {
            var url = $(this).attr('href');
            if (url != null) {
                getUrl(url);
            }
        }
        $('#load').show();
    });
    //下面是点击各种筛选条件之后调用ajax
    $(document).on('click', '.cj-con-list ul li,.cj-select-way ul li,.cj-select-time ul li,.cj-select-noon ul li,.cj-top-bot-hide', function (e) {
        $('#load').css({"display": "block"});
        var course_category=$('#category-select-popup').text();
        var class_modality=$('#way-select-popup').text();
        var course_time=$('#time-select-popup').text();
        var course_noon=$('#noon-select-popup').text();
        switch(course_time){
            case '周一':
                course_time='1';
                break;
            case '周二':
                course_time='2';
                break;
            case '周三':
                course_time='3';
                break;
            case '周四':
                course_time='4';
                break;
            case '周五':
                course_time='5';
                break;
            case '周六':
                course_time='6';
                break;
            case '周天':
                course_time='7';
        }
        switch (course_noon){
            case '上午':
                course_noon=0;
                break;
            case '下午':
                course_noon=1300;

        }
        $.post($('#card_route').val(), {
            course_category: course_category,
            class_modality: class_modality,
            course_time: course_time,
            course_noon:course_noon,
            ajax: 'course',
            _csrf: yii.getCsrfToken()
        }, function (data) {
            if (data.content != undefined) {
                $('#load').hide();
                $('#itemContainer').html(data.content);
                $('#p_touch_gain').html(data.data);
                $('#page').html(data.pages);
            }
        });

    });

    $(document).on('click', '.icon-chuqu', function (e) {
        $('#cj-touch-couse').hide()
    });
    //下面是按钮点击实现的效果
    $(document).on('click', '.icon-chuqu', function () {
        $('#cj-touch-couse').hide();
        $('#cj-couse-details').hide();
        window.location.reload();
    });
    $(document).on('click', '.icon-fanhui', function () {
        $('#cj-touch-couse').show();
        $('#cj-couse-details').hide();
    });


    $(document).on('click', '.list1', function () {
        var hidden_id = $(this).find('span').text();
        $('#load-details').show();
        $.post($('#card_route').val(), {
            hidden_id: hidden_id,
            ajax: 'details',
            _csrf: yii.getCsrfToken()
        }, function (data) {
            if (data.content != undefined) {
                $('#load-details').hide();
                $('.cj-content').html(data.content);
                $('.cj-details-banner').html(data.data)
            }
        });
        $('#cj-touch-couse').hide();
        $('#cj-couse-details').show();
    });
//下面是滑动页面上面的图片图标点击实现的效果
    $(document).on('click', '#top_img', function () {
        $('#cj-touch-couse').toggle();
    });
    $(document).on('click', '.close-img2', function () {
        $('.touch-card-no').hide();
    });

});

